<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<meta name="viewport" content="width=device-width,initial-scale=1.0,viewport-fit=cover">
		<title>中国地图</title>
		<link rel="shortcut icon" href=./favicon.ico> <link rel="stylesheet" type="text/css" href="css/hui.css" />
		<link rel="stylesheet" type="text/css" href="css/my.css" />
		<style type="text/css">
			.container {
				width: 100%;
				height: 100vh;
			}

			.chart {
				width: 100%;
				height: 100vh;
				background-image: url('./js/MapJs/images/beijingtupian.png');
				background-size: 100% 100%;
			}
		</style>


	</head>
	<body style="background-color: #00625A;">
		<div class="container">
			<div id="mapChart" class="chart"></div>
		</div>



		<script type="text/javascript" src="js/func/hui.js" charset="utf-8"></script>
		<!--<script type="text/javascript" src="js/func/mui.min.js" charset="utf-8"></script>-->
		<script type="text/javascript" src="js/obj/my.js" charset="utf-8"></script>
		<script type="text/javascript" src="./js/MapJs/echarts.min.js"></script>
		<script type="text/javascript" src="./js/MapJs/china.js"></script>
		<script type="text/javascript" src="./js/MapJs/jq.js"></script>
		<!--<script type="text/javascript" src="./js/MapJs/China_Map.js"></script>-->
		<script type="text/javascript" src="./js/MapJs/shi.js"></script>
		<script type="text/javascript" src="./js/MapJs/china-main-city-map.js"></script>
		<script>
			var shengJson = baseData[0].AZ; // 匹配省名
			var chinaJson = chinaData;

			var shengName = []; // 省名
			var cityBaseId = []; // 省id


			echarts.registerMap('china', chinaJson); //初始化地图数据
			var chart = echarts.init(document.getElementById('mapChart')); // 初始化ECharts画布

			var date = new Date();
			var year = date.getFullYear();
			var month = (date.getMonth() + 1) < 10 ? ('0' + (date.getMonth() + 1)) : (date.getMonth() + 1); //month_S获取当前月份  
			var day = date.getDate() < 10 ? ("0" + date.getDate()) : date.getDate(); //当前日期
			var currentDate = String(year) + String(month) + String(day);
			//			console.log('=========' + String(year) + String(month) + String(day)); 

			function getTaskTime(strDate) {
				//获取前七天日期函数
				var Seavedate = new Date(strDate);
				var y = Seavedate.getFullYear();
				var m = Seavedate.getMonth() + 1;
				//m = m < 10 ? ('0' + m) : m;
				if(m < 10){
					m = ('0' + m)
				}
				var d = Seavedate.getDate();
				//d = d < 10 ? ('0' + d) : d;
				if(d < 10){
					d = ('0' + d)
				}
				week = String(y) + String(m) + String(d);
				localStorage.setItem("refresDate", week); //缓存日期令牌
			}

			var RefreshDate = localStorage.getItem("refresDate"); //获取日期令牌
			//alert(RefreshDate);
			/*if(localStorage.hasOwnProperty("refresDate") || RefreshDate != null){ //当缓存时间令牌 存在
				if(currentDate > RefreshDate){ // 判断当前登录是否需要重新获取数据
					alert('11重新获取数据');
					getTaskTime(date.setDate(date.getDate() + 7));
					getColumns();
				}else{
					alert('有缓存数据'); 
					cacheData();
				}
				
			}else{//当缓存时间令牌 不存在   重新获取数据
				alert('22重新获取数据'); 
				getTaskTime(date.setDate(date.getDate() + 7));
				getColumns();
			}*/

			//过滤需要高亮的省份  然后重置ECharts
			function cacheData() {
				var dataColumns = JSON.parse(localStorage.getItem("dataColumns"));
				cityBaseId = []; //存放基地所在省id
				$.each(dataColumns, function(i, value) { //遍历每个省份的ID
					// console.log(value.deeppath);
					if (value.deeppath) {
						var DeepPath = value.deeppath.split('/');
						//DeepPath[0] == '' ? cityBaseId.push(DeepPath[1]) : cityBaseId.push(DeepPath[0]);
						if(DeepPath[0] == ''){
							cityBaseId.push(DeepPath[1]);
						}else{
							cityBaseId.push(DeepPath[0]);
						}
					}
				})
				var map = cityBaseId.reduce(function(m, x){
					if(x in m){
					    m[x]++
					}else{
					    m[x] = 1 
					}
					return m
				}, new Map()) 
				
				var baseFrequency = [], baseNumber = [];
				console.log(JSON.stringify(map));
				$.each(map, function(i, val) {
					baseFrequency.push(val)
					baseNumber.push(i)
				})
				$.each(shengJson, function(i, val) {
					if (val.code == baseNumber[0] || val.code == baseNumber[1]) {
						shengName.push(val.address);
					}
				})
				chinaMap();
				if (shengName.length == 1 && shengName[0] == '湖南') {
					chart.setOption({
						geo: {
							nameMap: {
								'湖南': shengName[0] + '(' + baseFrequency[0] + ')',
							},
							regions: [{
								name: shengName[0] + '(' + baseFrequency[0] + ')',
								itemStyle: {
									areaColor: 'red',
									color: 'red'
								}
							}, ],
						}
					})
				}
				if (shengName.length == 1 && shengName[0] == '新疆') {
					chart.setOption({
						geo: {
							nameMap: {
								'新疆': shengName[0] + '(' + baseFrequency[0] + ')',
							},
							regions: [{
								name: shengName[0] + '(' + baseFrequency[0] + ')',
								itemStyle: {
									areaColor: 'red',
									color: 'red'
								}
							}, ],
						}
					})
				}
				if (shengName.length > 1) {
					chart.setOption({
						geo: {
							nameMap: {
								'湖南': shengName[0] + '(' + baseFrequency[0] + ')',
								'新疆': shengName[1] + '(' + baseFrequency[1] + ')',
							},
							regions: [{
									name: shengName[0] + '(' + baseFrequency[0] + ')',
									itemStyle: {
										areaColor: 'red',
										color: 'red'
									}
								},
								{
									name: shengName[1] + '(' + baseFrequency[1] + ')',
									itemStyle: {
										areaColor: 'red',
										color: 'red'
									}
								}
							],
						}
					})
				}

			}
			getColumns()
			//获取数据方法
			function getColumns() {
				//var userId = 1;
				var userId = getValue('loginId');
				console.log(getValue('loginId'))
				//var url = baseURL() + "/gateway_handler.ashx?action=gateway_user"; 
				var url = newBaseUrl() + "/displaydirectory/getdisplaydirectorylistbyuser?userid=" + userId;
				var param = {
					userid: userId
				};

				/* console.log(url)
				console.log(param) */


				hui.loading("正在获取数据...");
				$.get(url, function(ret) {
					var result = ret;
					// console.log(result);

					cityBaseId = [];
					localStorage.setItem("dataColumns", JSON.stringify(result.result));
					cacheData();
					hui.closeLoading();
				})
			}

			//绘制ECharts方法
			function chinaMap() {

				chart.setOption({
					geo: {
						map: 'china',
						roam: true,
						zoom: 1.6,
						label: {
							normal: {
								show: true,
								color: '#fff'
							},
							emphasis: {
								textStyle: {
									color: '#fff'
								}
							}
						},

						itemStyle: {
							normal: {
								borderColor: '#1dc199', //省市边框线颜色
								areaColor: 'rgba(23, 27, 57,0)', //省市背景颜色
							},
							emphasis: {
								areaColor: null,
								shadowOffsetX: 0,
								shadowOffsetY: 0,
								shadowBlur: 20,
								borderWidth: 0,
								shadowColor: 'rgba(0, 0, 0, 0.5)'
							}
						}
					},
				});
				chart.on('click', function(param) {
					//	    			console.log(shengName);
					//	    			console.log(shengName.length);
					//	    			console.log(param.name);
					var name;
					var reg = new RegExp('[\u4e00-\u9fa5]+$', 'g');

					name = param.name.match(/[\u4e00-\u9fa5]/g).join("");
					var cityId = cityMap[name];
					//		        console.log(name);
					//		        console.log(cityId);
					if (shengName.length == 1 && shengName[0] == '湖南') {
						if (cityId == 430000) { //选中湖南需要市级数据
							hui.open("Province_Map.html?cityName=" + '湖南', {}, true, {});
							//var proninceRul = "Province_Map.html?cityName=" + '湖南';
							return;
						}
					}
					if (shengName.length == 1 && shengName[0] == '新疆') {
						if (cityId == 650000) { //选中新疆直接跳转到数据页
							hui.open("main_R.html?cityName=" + '吐鲁番', {}, true, {});
							return;
						}
					}
					if (shengName.length > 1) {
						if (cityId == 430000) { //选中湖南需要市级数据
							hui.open("Province_Map.html?cityName=" + '湖南', {}, true, {});
							//var proninceRul = "Province_Map.html?cityName=" + '湖南';
							return;
						}
						if (cityId == 650000) { //选中新疆直接跳转到数据页
							hui.open("main_New.html?cityName=" + '吐鲁番', {}, true, {});
							return;
						}
					}
					if (cityId != 430000 || cityId != 650000) {
						hui.upToast('当前选中省暂无基地！！');
					}
				})
			}
			
			function unique1(arr){//数组去重
			  	var hash=[];
			  	for (var i = 0; i < arr.length; i++) {
			  		if(hash.length == 0){
			  			hash.push(arr[i]);
			  		}
			     	if(hash.indexOf(arr[i])==-1){
			      		hash.push(arr[i]);
			     	}
			  	}
			  	return hash;
			}

			var huibackNum = 0;
			hui.Back = function() {
				if (huibackNum < 1) {
					hui.toast('再按一次退出程序');
					huibackNum++;
					setTimeout(function() {
						huibackNum = 0;
					}, 3000);
				} else {
					plus.runtime.quit();
				}
			}
		</script>
	</body>

</html>
